GetPointCountTransectID<-function(SamplingUnitID=NULL, transect_name=NULL, batch=FALSE,...) {
  
	if (is.null(SamplingUnitID) & is.null(transect_name)) stop("A SamplingUnitID or transect_name must be provided")
    if (!is.null(SamplingUnitID)) {
        transect.list<-paste("'",SamplingUnitID,"'",sep="",collapse=",")
        query<-paste(   "SELECT PointCountStationID as transect, SamplingUnitID as point",
                        "FROM SamplingUnitLegacy",  # TODO: Fix so that query is actually calling the current SamplingUnit table.  
                        "WHERE SamplingUnitTypeCd != 'PointCountStation'"
                )
       if (batch==FALSE) query<-paste(query,"AND SamplingUnitID in (",transect.list,")",sep="")
       
    } else {
        transect.list<-paste("'",transect_name,"'",sep="",collapse=",")
        query<- paste(  "SELECT PointCountStationID as transect, SamplingUnitName as point",
                        "FROM SamplingUnit",
                        "WHERE SamplingUnitTypeCd != 'PointCountStation'"
                )    
        if (batch==FALSE) query<-paste(query,"AND SamplingUnitName in (",transect.list,")",sep="")
    }
    query.results<-sqlQuery(db<-PRBOdbConnect(group="PRBOdb", ...),query)
    if (!is.null(SamplingUnitID)) output<-query.results$transect[match(SamplingUnitID,query.results$point)]
    if (!is.null(transect_name)) output<-query.results$transect[match(transect_name,query.results$point)]
    close(db)
    return(output)
}

# I do not believe this is ready for "batch" quite yet --- needs someway to match a vector of possible SamplingUnitIDs
#TODO: Generalize GetSamplingUnitID to work with all sampling unit types.
GetSamplingUnitID<-function(transect=NULL, transect_name=NULL, batch=FALSE, ...) {
    if (is.null(transect) & is.null(transect_name)) stop("A transect code or transect_name must be provided")
    if (!is.null(transect)) {
        transect.list<-paste("'",transect,"'",sep="",collapse=",")
        query<- paste(  "SELECT SamplingUnitID",
                        "FROM SamplingUnit",
                        "WHERE ShortName in (",transect.list,")"
                )
    } else {
        transect.list<-paste("'",transect_name,"'",sep="",collapse=",")
        query<- paste(  "SELECT SamplingUnitID",
                        "FROM SamplingUnit",
                        "WHERE FullName IN (",transect.list,")"
                )    
    }
    query.results<-sqlQuery(db<-PRBOdbConnect(group="PRBOdb", ...),query)
    close(db)
    return(query.results[,1])
}

GetTransectName<-function(transect=NULL, SamplingUnitID=NULL, batch=FALSE, ...) {
    if (is.null(transect) & is.null(SamplingUnitID)) stop ("Either a transect or a SamplingUnitID must be provided.")
    if (!is.null(transect)) {
        transect.list<-paste("'",transect,"'",sep="",collapse=",")
        query<- paste(  "SELECT DISTINCT SamplingUnitName",
                        "FROM SamplingUnitLegacy",
                        "WHERE SamplingUnitTypeCd != 'PointCountStation'",
                        "AND PointCountStationID IN (",transect.list,")"
        )
        query.results<-sqlQuery(db<-PRBOdbConnect(group="PRBOdb", ...),query)
        SamplingUnitName<-query.results[,1]
        close(db)
    } else {
        transect.list<-paste("'",SamplingUnitID,"'",sep="",collapse=",")
        query<-paste(   "SELECT DISTINCT SamplingUnitName",
                        "FROM SamplingUnitLegacy",
                        "WHERE SamplingUnitID in (",transect.list,")",
                        "AND SamplingUnitTypeCd != 'PointCountStation'"
                )
        query.results<-sqlQuery(db<-PRBOdbConnect(group="PRBOdb", ...),query)
        SamplingUnitName<-paste("'",query.results[,1],"'",sep="",collapse=",")
        close(db)
    }
    return(SamplingUnitName)        
}


#TODO: GetSpeciesCode needs to get cleaned up in order to handle single quotes
GetSpeciesCode<-function (species, sci.name=FALSE, ODBC=FALSE, batch=FALSE, ...) {
    if (is.null(species)==TRUE) stop("Error: No species name given")
    specquery.main<-paste("FROM FieldBird, FieldBirdTaxon, TaxonCommonName, Taxon",
                            "WHERE TaxonCommonName.TaxonId = FieldBirdTaxon.TaxonId",
                            "AND Taxon.TaxonId=FieldBirdTaxon.TaxonId",
                            "AND FieldBird.FieldBirdId = FieldBirdTaxon.FieldBirdId",
                            "AND FieldBird.BirdCdValidEndDt is NULL",
                            "AND language='English'")
    specquery.end<-""
    if (sci.name==FALSE) {
        specquery.start<-"SELECT TaxonCommonName.CommonName as species, FieldBird.BirdCd as spec"
        if (batch==FALSE) specquery.end<-paste("AND TaxonCommonName.CommonName IN (",paste("'",species,"'",sep="",collapse=","),")",sep="")
    } else {
        specquery.start<-"SELECT Taxon.TaxonName as species, FieldBird.BirdCd as spec"
        if (batch==FALSE) specquery.end<-paste("AND Taxon.TaxonName IN(",paste("'",species,"'",sep="", collapse=","),")",sep="")
    }    
    query<-paste(specquery.start,specquery.main,specquery.end,"GROUP BY species")
    query.results<-sqlQuery(db<-PRBOdbConnect(group="PRBOdb", ...),query)
#    merge.results<-merge(data.frame(species),query.results, all.x=TRUE,sort=FALSE,by="species")
#    SpeciesCode<-merge.results$spec
    SpeciesCode<-query.results$spec[match(species,query.results$species)]
    close(db)
    return(SpeciesCode)
}


#Perhaps this needs to be designed so it is one query and you just give it options to tell what you want out 
#(rather than lots of queries that are very specific)
GetSpeciesName<-function (spec, sci.name=FALSE, ODBC=FALSE, batch=FALSE, ...) {
    if(is.null(spec)==TRUE) stop("Error: No species code given")
    specquery.main<-paste("FROM FieldBird, FieldBirdTaxon, TaxonCommonName, Taxon",
                            "WHERE TaxonCommonName.TaxonId = FieldBirdTaxon.TaxonId",
                            "AND Taxon.TaxonId=FieldBirdTaxon.TaxonId",
                            "AND FieldBird.FieldBirdId = FieldBirdTaxon.FieldBirdId",
                            "AND FieldBird.BirdCdValidEndDt is NULL",
                            "AND language='English'")
    specquery.end<-""
    if (sci.name==FALSE) {
            specquery.start<-"SELECT TaxonCommonName.CommonName as species, FieldBird.BirdCd as spec"
        } else {
            specquery.start<-"SELECT Taxon.TaxonName as species, FieldBird.BirdCd as spec"
        }    
    if (batch==FALSE) specquery.end<-paste("AND FieldBird.BirdCd IN (",paste("'",spec,"'",sep="", collapse=","),")",sep="")
    query<-paste(specquery.start,specquery.main,specquery.end,"GROUP BY species")
    query.results<-sqlQuery(db<-PRBOdbConnect(group="PRBOdb", ...),query)
    SpeciesName<-query.results$species[match(spec,query.results$spec)]
#    merge.results<-merge(data.frame(spec),query.results, all.x=TRUE,sort=FALSE)
#    SpeciesName<-merge.results$species
    close(db)
    return(SpeciesName)
}
